from ultralytics.data.augment import RandomFlip
from ultralytics.data import YOLODataset
from ultralytics.cfg import cfg2dict
import random
import cv2

# 初始化RandomFlip类
randomflip = RandomFlip(p=1.0, direction='vertical')

# 输入数据labels
# 测试数据获取
config = cfg2dict('./coco8/coco8.yaml')
dataset = YOLODataset(data=config, img_path='./coco8/images')

# 随机选一张图片
idx = random.choices([0,1,2,3],k=1)[0]
idx = 2
labels = {"img":dataset.get_image_and_label(idx)['img'],
          "cls":dataset.get_image_and_label(idx)['cls'],
          "instances":dataset.get_image_and_label(idx)['instances']}

# 图片强度转换
results = randomflip(labels)
# 可视化保存
savedir = './randomflip_test.jpg'
cv2.imwrite(savedir, results['img'])